﻿using System;
using System.ComponentModel.Composition;
using System.Windows;
using MefBasic.Commans;
using Microsoft.Practices.Composite.Events;

namespace MefBasic
{
    [Export(typeof(ShellPresenter))]
    public class ShellPresenter:APresenter<IShellView>
    {
        [ImportingConstructor]
        public ShellPresenter(IShellView view, IEventAggregator eventAggregator) : base(view)
        {
            eventAggregator.GetEvent<BaseEvent<ShellDisabledChangedEventArgs>>().Subscribe(HandleShellDisabledChangedEvent);
        }

        private void HandleShellDisabledChangedEvent(ShellDisabledChangedEventArgs obj)
        {
            Disabled = obj.Disabled;
        }


        public bool Disabled
        {
            get { return (bool)GetValue(DisabledProperty); }
            set { SetValue(DisabledProperty, value); }
        }

        public static readonly DependencyProperty DisabledProperty =
            DependencyProperty.Register("Disabled", typeof(bool), typeof(ShellPresenter), new UIPropertyMetadata(false));



        public double ShellWidth
        {
            get { return (double)GetValue(ShellWidthProperty); }
            set { SetValue(ShellWidthProperty, value); }
        }

        public static readonly DependencyProperty ShellWidthProperty =
            DependencyProperty.Register("ShellWidth", typeof(double), typeof(ShellPresenter), new UIPropertyMetadata(0.0));

        public double ShellHeight
        {
            get { return (double)GetValue(ShellHeightProperty); }
            set { SetValue(ShellHeightProperty, value); }
        }

        public static readonly DependencyProperty ShellHeightProperty =
            DependencyProperty.Register("ShellHeight", typeof(double), typeof(ShellPresenter), new UIPropertyMetadata(0.0));


        public void Initialize(Window mainWindow)
        {
            mainWindow.SizeChanged += HandleWindowSizeChanged;
            HandleWindowSizeChanged(null, null);
        }

        private void HandleWindowSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if(Application.Current.MainWindow.WindowStyle==WindowStyle.None)
            {
                ShellHeight = Application.Current.MainWindow.ActualHeight;
                ShellWidth = Application.Current.MainWindow.ActualWidth;
            }
            else
            {
                ShellHeight = Application.Current.MainWindow.ActualHeight - 38;
                ShellWidth = Application.Current.MainWindow.ActualWidth - 18;
            }
        }
    }

    public class ShellDisabledChangedEventArgs:BaseEventArgs
    {
        public bool Disabled { get; set; }
    }
}
